package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupInfoService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupModelService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.SignupInfoMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.SignupModelJsonDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.SignupInfo;
import org.zfes.snowier.cloud.zbss.metting.biz.model.SignupModel;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class SignupInfoServiceImpl implements ISignupInfoService{
 	@Autowired
	private SignupInfoMapper signupInfoMapper;
	@Autowired
	private ISignupModelService signupModelService;
	
	
 	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void batchCreateSignupInfo(List<SignupInfo> SignupInfoList) {
 		signupInfoMapper.batchInsert(SignupInfoList);
 	}
 	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSignupInfo(Long metId, Long signupId,Map<String, Object> params) {
		Optional<SignupModel> signupModelOp= signupModelService.loadSignupModelByMetId(metId);
 		ZAssert.notEmpty(signupModelOp, "未查询到模板数据");
 		String formJson=signupModelOp.get().getFormJson();
 		
 		JSONArray formJsonArray = JSON.parseArray(formJson);
 		
 		List<SignupModelJsonDto> amodelList=formJsonArray.stream().map(obj -> {
 								JSONObject jsonObj = (JSONObject) obj; 
		 						return new SignupModelJsonDto( jsonObj.getString("type"),   jsonObj.getString("name"));
 					}).collect(Collectors.toList());
 		
 		List<SignupInfo> signupInfoList=Lists.newArrayList();
 		
 		for(int i=0 ; i< amodelList.size(); i++) {
 			
 			SignupModelJsonDto amodel=amodelList.get(i);
 			String fieldName=amodel.getName();
 			Object fieldValue=params.get(fieldName);
 			SignupInfo signupInfo=
 					new SignupInfo().setSignupId(signupId).setMetId(metId).setSortNum(i)
 					.setFieldName(fieldName);
 			String fv=fieldValue==null?"":String.valueOf(fieldValue);
 			if(fv.length()>800) {
 				signupInfo.setFieldValueText(fv);
 			}else {
 				signupInfo.setFieldValue(fv);
 			}
 			signupInfoList.add(signupInfo);
 		}
		signupInfoMapper.batchInsert(signupInfoList);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateSignupInfo(SignupInfo SignupInfo) {
		ZBeanUtil.setUpdatedInfo(SignupInfo);
		ZBeanUtil.validateBean(SignupInfo);
		signupInfoMapper.updateByPrimaryKeySelective(SignupInfo);
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SignupInfo> loadSignupInfoById(Long id) {
		if(id!=null){
			return Optional.ofNullable(signupInfoMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
 	@Override
	public  List<SignupInfo> loadBySignupId(Long signupId){
 		if(signupId!=null){
			return signupInfoMapper.selectBySignupId(signupId);
		}
		return Lists.newArrayList();
 	}
	
	

	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteSignupInfo(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		signupInfoMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setSignupInfoStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		signupInfoMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadSignupInfoDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(signupInfoMapper.selectListCount(pm), signupInfoMapper.selectMapListPage(pm));
		return ds;
	}


	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SignupInfo> loadBySignupIdList(List<Long> signupIdList) {
		if(ZObjectUtil.isEmpty(signupIdList)) {
			return Lists.newArrayList();
		}
		
		return signupInfoMapper.selectBySignupIdList( signupIdList);
	}
	
}